﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
namespace vashSMJ
{
    public class EventManager
    {
        private EventManager(){}
        private static EventManager instance;//唯一单例
        public static EventManager getInstance(){
            if(instance == null){
                instance = new EventManager();
            }
            return instance;
        }

        private void emptyHandler(){}
        private Dictionary<string,Action> eventDic = new Dictionary<string, Action>();

        /// <summary>
        /// 给需要监听的地方注册监听
        /// </summary>
        /// <param name="EventName"></param> 定义事件名字 eg：PlayerDieEvent
        /// <param name="callback"></param> 传入委托回调函数 eg: delegate(){xxxxx;}
        public void registerEventListener(string EventName,Action callback){
            if (eventDic.ContainsKey(EventName))
            {
               eventDic[EventName]+=callback;
            }
            else
            {
                eventDic.Add(EventName,new Action(emptyHandler));
                eventDic[EventName]+=callback;
            }
        }
        
        /// <summary>
        /// 调用相应事件的回调函数
        /// </summary>
        /// <param name="EventName"></param> 事件名字
        public void publishEvent(string EventName){
            if(eventDic.ContainsKey(EventName)){
                Action funcs = eventDic[EventName];
                funcs();
            }
            else{
                Debug.LogError("no this Event in EventSystem");
            }
            
        }


    }


}